﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class VipBenefitDailyReward : MonoBehaviour
    {
        public UILabel labTitle;
        public UILabel labWordOnBtn;
        public GameObject btnGet;
        public GameObject getted;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;

        public UIActivityVipBenefits vipBenefits;
        
        bool isGoRecharge = false;

        public void SetRewardInfo(ActivityDailyBoonItem item, bool onlyShow = false)
        {
            int vip = StaticData.playerData.vip;
            string color = "[0b5dc2]";
            if (item.vip > vip)
                color = "[e48403]";
            labTitle.text = string.Format(TableTextBlo.GetText("vip_x_only_gift"), color, item.vip);

            foreach (var c in simpleGrid.childs)
            {
                Destroy(c);
            }
            simpleGrid.childs.Clear();

            GameObject commonicon = AssetLoad.ResourceManager.Instance.LoadResource("prefab/CommonIcon") as GameObject;
            foreach (var i in item.reward)
            {
                GameObject obj = Instantiate(commonicon);
                obj.GetComponent<CommonIcon>().SetData(i.id, true, i.num);
                simpleGrid.AddChild(obj);
            }
            scrollView.ResetPosition();

            btnGet.SetActive(false);
            getted.SetActive(false);
            if(!onlyShow)
            {
                isGoRecharge = false;
                if (item.status == 1)
                    btnGet.SetActive(true);
                else if (item.status == 2)
                    getted.SetActive(true);
            }
            else
            {
                if (item.vip > vip)
                {
                    btnGet.SetActive(true);
                    isGoRecharge = true;
                }
            }
        }

        public void OnBtnClick()
        {
            if(isGoRecharge)
            {
                PageManager.instance.OpenPage("ChargePage", "");
                return;
            }
            CSGetVipDailyBoonReward msg = new CSGetVipDailyBoonReward();
            NetAPI.SendHttp(OpDefine.CSGetVipDailyBoonReward, msg, OnMsgRecv);
        }

        void OnMsgRecv(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCGetVipDailyBoonReward msg = packet.kBody as SCGetVipDailyBoonReward;
                StaticData.playerData.AddResultObj(msg.get);
                
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("get_success"));

                vipBenefits.UpdateDailyTab(msg.daily);
                StaticData.redTips.ReduceActivitys();
                StaticData.redTips.SetPointState(RedPointTip.Red.vipBoon, 0);
            }
        }
    }
}

